//
//  CircleViewController.m
//  Lucky28
//
//  Created by zr on 2018/7/31.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "CircleViewController.h"
#import "CircleTableViewCell.h"
#import "CircleHeaderView.h"
#import "PushCircleViewController.h"
#import "CircleInfoViewController.h"
#import "CircleModel.h"

@interface CircleViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray<CircleModel *>* CircleListArray;
@property (nonatomic, copy)NSString * essence;

@end

@implementation CircleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];

    self.essence = @"1";
    
    [CircleModel circleWithEssence:self.essence WithSucess:^(NSArray<CircleModel *> *array) {
        
        self.CircleListArray = array;
    } error:^{
        
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(RefreshCircleData:) name:@"RefreshCircleData" object:nil];
    
    [self setupNav];
    [self setupUI];
}


- (void)setCircleListArray:(NSArray<CircleModel *> *)CircleListArray{
    
    _CircleListArray = CircleListArray;
    
    [self.tableView reloadData];
}


- (void)setupNav{
    
    UIButton * backButton1 = [[UIButton alloc] init];
    
    //    backButton.frame = CGRectMake(0 , 0 , 65 * SCREENH6, 20 * SCREENH6);
//    [backButton1 setTitle:@"登录" forState:UIControlStateNormal];
    [backButton1 setImage:ImageNamed(@"写帖子") forState:UIControlStateNormal];
    [backButton1 addTarget:self action:@selector(pushToShareMessage) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * nextButton = [[UIBarButtonItem alloc] initWithCustomView:backButton1];
    
    self.navigationItem.rightBarButtonItem = nextButton;
    
}



- (void)setupUI{
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT - 80) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.rowHeight = 85 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[CircleTableViewCell class] forCellReuseIdentifier:@"CircleListCell"];
    [self.tableView registerClass:[CircleHeaderView class] forHeaderFooterViewReuseIdentifier:@"CircleHeaderView"];
    
    [self.view addSubview:self.tableView];
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.CircleListArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CircleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CircleListCell" forIndexPath:indexPath];
    
    if (indexPath.row == 0) {
        
        cell.explationLabel.hidden = NO;
    }else{
        
        cell.explationLabel.hidden = YES;
    }
    
        cell.model = self.CircleListArray[indexPath.row];
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 45 * SCREENH6;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    
    CircleHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"CircleHeaderView"];
    
    return header;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HIDDEN_TAB;

    CircleInfoViewController * infoVC = [CircleInfoViewController new];
    
    CircleModel * model = self.CircleListArray[indexPath.row];
    
    infoVC.ID = model.ID;
    
    [self.navigationController pushViewController:infoVC animated:YES];
    
    SHOW_TAB;
}



- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return YES;
}

// 定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

// 进入编辑模式，按下出现的编辑按钮后,进行删除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        CircleModel * model = self.CircleListArray[indexPath.row];
        
        NSString * ID = model.ID;
        NSString * token = DEF_GET_OBJECT(@"token");
        
        NSDictionary * dict = @{@"token":token,@"note_id":ID,@"shield":@"1"};
        
        [[NetworkTool sharedTools] POST:@"/index/Note/shield" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
//            NSLog(@"===%@",responseObject);
            int status = [responseObject[@"status"] intValue];
            
            if (status == 200) {
                
                [CircleModel circleWithEssence:self.essence WithSucess:^(NSArray<CircleModel *> *array) {
                    
                    self.CircleListArray = array;
                } error:^{
                    
                }];
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
        
    }
}

// 修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return @"屏蔽";
    
}


- (void)pushToShareMessage{
    
    HIDDEN_TAB;
    
    PushCircleViewController * pushVC = [PushCircleViewController new];
    
    [self.navigationController pushViewController:pushVC animated:YES];
    
    SHOW_TAB;
}


- (void)RefreshCircleData:(NSNotification *)noti{
    
    self.essence = noti.userInfo[@"type"];
    
    [CircleModel circleWithEssence:self.essence WithSucess:^(NSArray<CircleModel *> *array) {
        
        self.CircleListArray = array;
    } error:^{
        
    }];
}


- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
